package com.lagou.java.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;

//可以让我们优雅底部或Controller层抛出的异常
@ControllerAdvice
public class GlobalExceptionResolver {

    @ExceptionHandler(ArithmeticException.class)
    public ModelAndView handleException (ArithmeticException exception, HttpServletResponse response) {
//        try {
//            response.getWriter().write("=======>" + exception.getMessage());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        ModelAndView modelAndView = new ModelAndView().addObject("msg", exception.getMessage());
        modelAndView.setViewName("error");
        return modelAndView;
    }



}
